package bar.dark.lee.setting.viewtype;

import java.util.ArrayList;

import org.apache.http.params.CoreConnectionPNames;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
import bar.dark.lee.BDConstents;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.RadioDialogActivity;
import bar.dark.lee.common.SuperInterface;
import bar.dark.lee.insert.InsertInActivity;
import bar.dark.lee.setting.TipInfoActivity;
import bar.dark.lee.setting.TipsActivity;
import bar.dark.lee.widget.WidgetArrayList;
import bar.dark.lee.widget.WidgetGetSqlQuery;

public class MainViewTypeActivity extends SuperActivity implements SuperInterface 
{
	private static final int REQUEST_MAIN_1 = 0;
	private static final int REQUEST_MAIN_2 = 1;
	private static final int REQUEST_MAIN_3 = 2;
	private static final int REQUEST_MAIN_4 = 3;
	
	private static final int REQUEST_MAIN_5 = 4;
	private static final int REQUEST_MAIN_6 = 5;
	private static final int REQUEST_MAIN_7 = 6;
	
	private static final int REQUEST_MAIN_8 = 7;
	private static final int REQUEST_MAIN_9 = 8;
	private static final int REQUEST_MAIN_10 = 9;

	private View chk1;
	private View chk2;
	private View chk3;
	private View chk4;
	
	private View chk5;
	private View chk6;
	private View chk7;
	
	private View chk8;
	private View chk9;
	private View chk10;
	
	private TextView mainTextView1;
	private TextView mainTextView2;
	private TextView mainTextView3;
	private TextView mainTextView4;
	
	private TextView mainTextView5;
	private TextView mainTextView6;
	private TextView mainTextView7;
	private TextView mainTextView8;
	private TextView mainTextView9;
	private TextView mainTextView10;
	
	private Button btnMainDefault;
	private Button btnMainSave;
	
	private SharedPreferences pref;
	private SharedPreferences.Editor editor;
	
	private String main1_key = "-1";
	private String main2_key = "-1";
	private String main3_key = "-1";
	private String main4_key = "-1";
	
	private String main5_key = "-1";
	private String main6_key = "-1";
	private String main7_key = "-1";
	
	private String main8_key = "-1";
	private String main9_key = "-1";
	private String main10_key = "-1";
	
	private WidgetArrayList widgetList;
	
	private String [] mainKey;
	
	private WidgetGetSqlQuery widgetSql;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_mainviewtype_activity);
		
		widgetSql = new WidgetGetSqlQuery(this);
		
		setTitle("메인 설정");
		
		setFindView();
		setClickListener();
		
		new ShowDialog().execute(BDUtil.asyncFlag, 0);
	}
	
	@Override
	protected void tipButton() 
	{
		Intent intent = new Intent(MainViewTypeActivity.this, TipInfoActivity.class);
		intent.putExtra(TipsActivity.TIP_REQUEST_KEY, TipsActivity.TIP_VIEWTYPE);
		startActivity(intent);
	}
	
	@Override
	public void setFindView() 
	{
		chk1 = findViewById(R.id.settingViewType1);
		chk2 = findViewById(R.id.settingViewType2);
		chk3 = findViewById(R.id.settingViewType3);
		chk4 = findViewById(R.id.settingViewType4);
		
		chk5 = findViewById(R.id.settingViewMonthType1);
		chk6 = findViewById(R.id.settingViewMonthType2);
		chk7 = findViewById(R.id.settingViewMonthType3);
		
		chk8 = findViewById(R.id.settingViewTodayType1);
		chk9 = findViewById(R.id.settingViewTodayType2);
		chk10 = findViewById(R.id.settingViewTodayType3);
		
		mainTextView1 = (TextView) findViewById(R.id.settingViewTypeTextView1);
		mainTextView2 = (TextView) findViewById(R.id.settingViewTypeTextView2);
		mainTextView3 = (TextView) findViewById(R.id.settingViewTypeTextView3);
		mainTextView4 = (TextView) findViewById(R.id.settingViewTypeTextView4);
		
		mainTextView5 = (TextView) findViewById(R.id.settingViewMonthTypeTextView1);
		mainTextView6 = (TextView) findViewById(R.id.settingViewMonthTypeTextView2);
		mainTextView7 = (TextView) findViewById(R.id.settingViewMonthTypeTextView3);
		
		mainTextView8 = (TextView) findViewById(R.id.settingViewTodayTypeTextView1);
		mainTextView9 = (TextView) findViewById(R.id.settingViewTodayTypeTextView2);
		mainTextView10 = (TextView) findViewById(R.id.settingViewTodayTypeTextView3);
		
		btnMainDefault = (Button) findViewById(R.id.settingViewType_default);
		btnMainSave = (Button) findViewById(R.id.settingViewType_btnMainSave);
	}
	
	@Override
	public void setClickListener() 
	{
		chk1.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MainViewDialogActivity.class);
				intent.putExtra(MainViewDialogActivity.REQUEST_CODE, REQUEST_MAIN_1);
				intent.putExtra(MainViewDialogActivity.RADIOSELECT, mainTextView1.getText().toString());
				startActivityForResult(intent, REQUEST_MAIN_1);
			}
		});
		
		chk2.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MainViewDialogActivity.class);
				intent.putExtra(MainViewDialogActivity.REQUEST_CODE, REQUEST_MAIN_2);
				intent.putExtra(MainViewDialogActivity.RADIOSELECT, mainTextView2.getText().toString());
				startActivityForResult(intent, REQUEST_MAIN_2);
			}
		});
		
		chk3.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MainViewDialogActivity.class);
				intent.putExtra(MainViewDialogActivity.REQUEST_CODE, REQUEST_MAIN_3);
				intent.putExtra(MainViewDialogActivity.RADIOSELECT, mainTextView3.getText().toString());
				startActivityForResult(intent, REQUEST_MAIN_3);
			}
		});
		
		chk4.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MainViewDialogActivity.class);
				intent.putExtra(MainViewDialogActivity.REQUEST_CODE, REQUEST_MAIN_4);
				intent.putExtra(MainViewDialogActivity.RADIOSELECT, mainTextView4.getText().toString());
				startActivityForResult(intent, REQUEST_MAIN_4);
			}
		});
		
		chk5.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MainViewDialogActivity.class);
				intent.putExtra(MainViewDialogActivity.REQUEST_CODE, REQUEST_MAIN_5);
				intent.putExtra(MainViewDialogActivity.RADIOSELECT, mainTextView5.getText().toString());
				startActivityForResult(intent, REQUEST_MAIN_5);
			}
		});
		
		chk6.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MainViewDialogActivity.class);
				intent.putExtra(MainViewDialogActivity.REQUEST_CODE, REQUEST_MAIN_6);
				intent.putExtra(MainViewDialogActivity.RADIOSELECT, mainTextView6.getText().toString());
				startActivityForResult(intent, REQUEST_MAIN_6);
			}
		});
		
		chk7.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MainViewDialogActivity.class);
				intent.putExtra(MainViewDialogActivity.REQUEST_CODE, REQUEST_MAIN_7);
				intent.putExtra(MainViewDialogActivity.RADIOSELECT, mainTextView7.getText().toString());
				startActivityForResult(intent, REQUEST_MAIN_7);
			}
		});
		
		chk8.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MainViewDialogActivity.class);
				intent.putExtra(MainViewDialogActivity.REQUEST_CODE, REQUEST_MAIN_8);
				intent.putExtra(MainViewDialogActivity.RADIOSELECT, mainTextView8.getText().toString());
				startActivityForResult(intent, REQUEST_MAIN_8);
			}
		});
		
		chk9.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MainViewDialogActivity.class);
				intent.putExtra(MainViewDialogActivity.REQUEST_CODE, REQUEST_MAIN_9);
				intent.putExtra(MainViewDialogActivity.RADIOSELECT, mainTextView9.getText().toString());
				startActivityForResult(intent, REQUEST_MAIN_9);
			}
		});
		
		chk10.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MainViewDialogActivity.class);
				intent.putExtra(MainViewDialogActivity.REQUEST_CODE, REQUEST_MAIN_10);
				intent.putExtra(MainViewDialogActivity.RADIOSELECT, mainTextView10.getText().toString());
				startActivityForResult(intent, REQUEST_MAIN_10);
			}
		});
		
		btnMainDefault.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				mainTextView1.setText(widgetList.get(0));
				mainTextView2.setText(widgetList.get(1));
				mainTextView3.setText(widgetList.get(2));
				mainTextView4.setText(widgetList.get(3));
				mainTextView5.setText(widgetList.get(6));
				mainTextView6.setText(widgetList.get(4));
				mainTextView7.setText(widgetList.get(5));
				mainTextView8.setText(widgetList.get(9));
				mainTextView9.setText(widgetList.get(7));
				mainTextView10.setText(widgetList.get(8));
				
				main1_key = "-1";
				main2_key = "-1";
				main3_key = "-1";
				main4_key = "-1";
				main5_key = "-1";
				main6_key = "-1";
				main7_key = "-1";
				main8_key = "-1";
				main9_key = "-1";
				main10_key = "-1";
				
				BDUtil.showToast(superContext, "기본 값으로 변경 되었습니다. 저장해주세요.");
			}
		});
		
		btnMainSave.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				BDUtil.showSelectDialog(MainViewTypeActivity.this, "알림", "메인 표시항목을 변경 하시겠습니까?", 
						new DialogInterface.OnClickListener() 
				{
					@Override
					public void onClick(DialogInterface dialog, int which) 
					{
						new ShowDialog().execute(BDUtil.asyncFlag, 1);
						dialog.dismiss();
					}
				});
			}
		});
	}
	
	@Override
	public void setDataSetting() 
	{
		pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		editor = pref.edit();
		
		widgetList = new WidgetArrayList();
		
		String bigMain = pref.getString(BDConstents.BIG_MAIN_SETTING, "0,1,2,3,6,4,5,9,7,8"); 
		
		mainKey = bigMain.split(",");
		
		if (mainKey.length < 10 )
		{
			bigMain = "0,1,2,3,6,4,5,9,7,8";
			mainKey = bigMain.split(",");
		}
		
		setBigMainKey();
	}
	
	private void setBigMainKey()
	{
		if (mainKey[0].equals(WidgetGetSqlQuery.BANKBOOK)
			|| mainKey[0].equals(WidgetGetSqlQuery.CHECKCARD)
			|| mainKey[0].equals(WidgetGetSqlQuery.CREDITCARD)
			|| mainKey[0].equals(WidgetGetSqlQuery.SECRET))
		{
			main1_key = mainKey[0];
		}
		else
		{
			main1_key = "-1";
		}
		
		if (mainKey[1].equals(WidgetGetSqlQuery.BANKBOOK)
				|| mainKey[1].equals(WidgetGetSqlQuery.CHECKCARD)
				|| mainKey[1].equals(WidgetGetSqlQuery.CREDITCARD)
				|| mainKey[1].equals(WidgetGetSqlQuery.SECRET))
		{
			main2_key = mainKey[1];
		}
		else
		{
			main2_key = "-1";
		}
		
		if (mainKey[2].equals(WidgetGetSqlQuery.BANKBOOK)
				|| mainKey[2].equals(WidgetGetSqlQuery.CHECKCARD)
				|| mainKey[2].equals(WidgetGetSqlQuery.CREDITCARD)
				|| mainKey[2].equals(WidgetGetSqlQuery.SECRET))
		{
			main3_key = mainKey[2];
		}
		else
		{
			main3_key = "-1";
		}
		
		if (mainKey[3].equals(WidgetGetSqlQuery.BANKBOOK)
				|| mainKey[3].equals(WidgetGetSqlQuery.CHECKCARD)
				|| mainKey[3].equals(WidgetGetSqlQuery.CREDITCARD)
				|| mainKey[3].equals(WidgetGetSqlQuery.SECRET))
		{
			main4_key = mainKey[3];
		}
		else
		{
			main4_key = "-1";
		}
		
		if (mainKey[4].equals(WidgetGetSqlQuery.BANKBOOK)
				|| mainKey[4].equals(WidgetGetSqlQuery.CHECKCARD)
				|| mainKey[4].equals(WidgetGetSqlQuery.CREDITCARD)
				|| mainKey[4].equals(WidgetGetSqlQuery.SECRET))
		{
			main5_key = mainKey[4];
		}
		else
		{
			main5_key = "-1";
		}
		
		if (mainKey[5].equals(WidgetGetSqlQuery.BANKBOOK)
				|| mainKey[5].equals(WidgetGetSqlQuery.CHECKCARD)
				|| mainKey[5].equals(WidgetGetSqlQuery.CREDITCARD)
				|| mainKey[5].equals(WidgetGetSqlQuery.SECRET))
		{
			main6_key = mainKey[5];
		}
		else
		{
			main6_key = "-1";
		}
		
		if (mainKey[6].equals(WidgetGetSqlQuery.BANKBOOK)
				|| mainKey[6].equals(WidgetGetSqlQuery.CHECKCARD)
				|| mainKey[6].equals(WidgetGetSqlQuery.CREDITCARD)
				|| mainKey[6].equals(WidgetGetSqlQuery.SECRET))
		{
			main7_key = mainKey[6];
		}
		else
		{
			main7_key = "-1";
		}
		
		if (mainKey[7].equals(WidgetGetSqlQuery.BANKBOOK)
				|| mainKey[7].equals(WidgetGetSqlQuery.CHECKCARD)
				|| mainKey[7].equals(WidgetGetSqlQuery.CREDITCARD)
				|| mainKey[7].equals(WidgetGetSqlQuery.SECRET))
		{
			main8_key = mainKey[7];
		}
		else
		{
			main8_key = "-1";
		}
		
		if (mainKey[8].equals(WidgetGetSqlQuery.BANKBOOK)
				|| mainKey[8].equals(WidgetGetSqlQuery.CHECKCARD)
				|| mainKey[8].equals(WidgetGetSqlQuery.CREDITCARD)
				|| mainKey[8].equals(WidgetGetSqlQuery.SECRET))
		{
			main9_key = mainKey[8];
		}
		else
		{
			main9_key = "-1";
		}
		
		if (mainKey[9].equals(WidgetGetSqlQuery.BANKBOOK)
				|| mainKey[9].equals(WidgetGetSqlQuery.CHECKCARD)
				|| mainKey[9].equals(WidgetGetSqlQuery.CREDITCARD)
				|| mainKey[9].equals(WidgetGetSqlQuery.SECRET))
		{
			main10_key = mainKey[9];
		}
		else
		{
			main10_key = "-1";
		}
	}
	
	private void saveMainType()
	{
		String key = "";
		
		if (main1_key.equals("-1"))
		{
			for (int i = 0; i < widgetList.size(); i++)
			{
				String name = widgetList.get(i);
				
				if (mainTextView1.getText().toString().equals(name))
				{
					key += i + ",";
				}
			}
			
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND1, "");
		}
		else
		{
			key += main1_key + ",";
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND1, 
					mainTextView1.getText().toString());
		}
		
		if (main2_key.equals("-1"))
		{
			for (int i = 0; i < widgetList.size(); i++)
			{
				String name = widgetList.get(i);
				
				if (mainTextView2.getText().toString().equals(name))
				{
					key += i + ",";
				}
			}
			
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND2, "");
		}
		else
		{
			key += main2_key + ",";
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND2, 
					mainTextView2.getText().toString());
		}
		
		if (main3_key.equals("-1"))
		{
			for (int i = 0; i < widgetList.size(); i++)
			{
				String name = widgetList.get(i);
				
				if (mainTextView3.getText().toString().equals(name))
				{
					key += i + ",";
				}
			}
			
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND3, "");
		}
		else
		{
			key += main3_key + ",";
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND3, 
					mainTextView3.getText().toString());
		}
		
		if (main4_key.equals("-1"))
		{
			for (int i = 0; i < widgetList.size(); i++)
			{
				String name = widgetList.get(i);
				
				if (mainTextView4.getText().toString().equals(name))
				{
					key += i + ",";
				}
			}
			
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND4, "");
		}
		else
		{
			key += main4_key + ",";
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND4, 
					mainTextView4.getText().toString());
		}
		
		if (main5_key.equals("-1"))
		{
			for (int i = 0; i < widgetList.size(); i++)
			{
				String name = widgetList.get(i);
				
				if (mainTextView5.getText().toString().equals(name))
				{
					key += i + ",";
				}
			}
			
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND5, "");
		}
		else
		{
			key += main5_key + ",";
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND5, 
					mainTextView5.getText().toString());
		}
		
		if (main6_key.equals("-1"))
		{
			for (int i = 0; i < widgetList.size(); i++)
			{
				String name = widgetList.get(i);
				
				if (mainTextView6.getText().toString().equals(name))
				{
					key += i + ",";
				}
			}
			
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND6, "");
		}
		else
		{
			key += main6_key + ",";
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND6, 
					mainTextView6.getText().toString());
		}
		
		if (main7_key.equals("-1"))
		{
			for (int i = 0; i < widgetList.size(); i++)
			{
				String name = widgetList.get(i);
				
				if (mainTextView7.getText().toString().equals(name))
				{
					key += i + ",";
				}
			}
			
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND7, "");
		}
		else
		{
			key += main7_key + ",";
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND7, 
					mainTextView7.getText().toString());
		}
		
		if (main8_key.equals("-1"))
		{
			for (int i = 0; i < widgetList.size(); i++)
			{
				String name = widgetList.get(i);
				
				if (mainTextView8.getText().toString().equals(name))
				{
					key += i + ",";
				}
			}
			
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND8, "");
		}
		else
		{
			key += main8_key + ",";
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND8, 
					mainTextView8.getText().toString());
		}
		
		if (main9_key.equals("-1"))
		{
			for (int i = 0; i < widgetList.size(); i++)
			{
				String name = widgetList.get(i);
				
				if (mainTextView9.getText().toString().equals(name))
				{
					key += i + ",";
				}
			}
			
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND9, "");
		}
		else
		{
			key += main9_key + ",";
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND9, 
					mainTextView9.getText().toString());
		}
		
		if (main10_key.equals("-1"))
		{
			for (int i = 0; i < widgetList.size(); i++)
			{
				String name = widgetList.get(i);
				
				if (mainTextView10.getText().toString().equals(name))
				{
					key += i + "";
				}
			}
			
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND10, "");
		}
		else
		{
			key += main10_key + "";
			editor.putString(BDConstents.BIG_MAIN_MONEYKIND10, 
					mainTextView10.getText().toString());
		}
		
		editor.putString(BDConstents.BIG_MAIN_SETTING, key);
		
		editor.commit();
	
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode == Activity.RESULT_OK)
		{
			String selectData = data.getStringExtra(RadioDialogActivity.RADIOSTRING);
			
			if (selectData.equals("없음"))
			{
				BDUtil.showToast(superContext, "표시할 항목이 없습니다. 다시 선택해 주세요.");
				return;
			}
			
			if (requestCode == REQUEST_MAIN_1)
			{
				main1_key = data.getStringExtra(MainViewDialogActivity.MAIN_TYPE_KEY);
				mainTextView1.setText(selectData);
			}
			else if (requestCode == REQUEST_MAIN_2)
			{
				main2_key = data.getStringExtra(MainViewDialogActivity.MAIN_TYPE_KEY);
				mainTextView2.setText(selectData);
			}
			else if (requestCode == REQUEST_MAIN_3)
			{
				main3_key = data.getStringExtra(MainViewDialogActivity.MAIN_TYPE_KEY);
				mainTextView3.setText(selectData);
			}
			else if (requestCode == REQUEST_MAIN_4)
			{
				main4_key = data.getStringExtra(MainViewDialogActivity.MAIN_TYPE_KEY);
				mainTextView4.setText(selectData);
			}
			else if (requestCode == REQUEST_MAIN_5)
			{
				main5_key = data.getStringExtra(MainViewDialogActivity.MAIN_TYPE_KEY);
				mainTextView5.setText(selectData);
			}
			else if (requestCode == REQUEST_MAIN_6)
			{
				main6_key = data.getStringExtra(MainViewDialogActivity.MAIN_TYPE_KEY);
				mainTextView6.setText(selectData);
			}
			else if (requestCode == REQUEST_MAIN_7)
			{
				main7_key = data.getStringExtra(MainViewDialogActivity.MAIN_TYPE_KEY);
				mainTextView7.setText(selectData);
			}
			else if (requestCode == REQUEST_MAIN_8)
			{
				main8_key = data.getStringExtra(MainViewDialogActivity.MAIN_TYPE_KEY);
				mainTextView8.setText(selectData);
			}
			else if (requestCode == REQUEST_MAIN_9)
			{
				main9_key = data.getStringExtra(MainViewDialogActivity.MAIN_TYPE_KEY);
				mainTextView9.setText(selectData);
			}
			else if (requestCode == REQUEST_MAIN_10)
			{
				main10_key = data.getStringExtra(MainViewDialogActivity.MAIN_TYPE_KEY);
				mainTextView10.setText(selectData);
			}
		}
	}
	
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			if (BDUtil.btnEnable)
			{
				BDUtil.btnEnable = false;
				BDUtil.asyncFlag = -1;
				
				dialog = new ProgressDialog(MainViewTypeActivity.this);
				dialog.setCancelable(false);
				dialog.setMessage("Loading...");
				dialog.show();
			}
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			if (arg0[0] == 0)
			{
				if (arg0[1] == 0)
				{	// 초기셋팅
					setDataSetting();
					return 0;
				}
				else if (arg0[1] == 1)
				{	// 메인 저장
					saveMainType();
					return 1;
				}
			}
			
			return null;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			
			dialog.dismiss();
			
			if (result == 0)
			{
				BDUtil.btnEnable = true;
				BDUtil.asyncFlag = 0;
				
				mainTextView1.setText(widgetSql.getName(mainKey[0], 0, 1));
				mainTextView2.setText(widgetSql.getName(mainKey[1], 1, 1));
				mainTextView3.setText(widgetSql.getName(mainKey[2], 2, 1));
				mainTextView4.setText(widgetSql.getName(mainKey[3], 3, 1));
				
				mainTextView5.setText(widgetSql.getName(mainKey[4], 4, 1));
				mainTextView6.setText(widgetSql.getName(mainKey[5], 5, 1));
				mainTextView7.setText(widgetSql.getName(mainKey[6], 6, 1));
				
				mainTextView8.setText(widgetSql.getName(mainKey[7], 7, 1));
				mainTextView9.setText(widgetSql.getName(mainKey[8], 8, 1));
				mainTextView10.setText(widgetSql.getName(mainKey[9], 9, 1));
			}
			else if (result == 1)
			{	// 메인 저장
				BDUtil.showDialogFinish(MainViewTypeActivity.this, "알림", "메인 표시항목이 변경되었습니다.");
			}
		}
	}
}
